package com.taikang.system.controller.admin;

import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.json.simple.JSONObject;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.taikang.base.controller.BaseController;
import com.taikang.base.token.TokenValidate;
import com.taikang.system.dto.SysRegionCondition;
import com.taikang.system.entity.SysRegionArea;
import com.taikang.system.form.SysRegionAreaForm;
import com.taikang.system.form.SysRegionForm;
import com.taikang.system.service.ISysRegionAreaService;
import com.taikang.system.service.ISysRegionService;

/**
 * 大区管理
 * @author taikang
 * @create-time 2015-10-7   下午10:33:14
 * @version 1.0
 */
@Controller("sysRegionAreaController")
@Scope("prototype")
@RequestMapping(value = "/admin/system/region/area")
public class SysRegionAreaController extends BaseController {

	@Resource(name = "sysRegionAreaServiceImpl")
	private ISysRegionAreaService sysRegionAreaService;
	@Resource(name = "sysRegionServiceImpl")
	private ISysRegionService sysRegionService;


	/**
	 * 首页
	 * @return 
	 */
	@RequestMapping(value = "/index.htm", method = RequestMethod.GET)
	public String index(){
		return "/admin/system/sys_region_area_index";
	}
	/**
	 * 跳转到添加页面
	 * @return 
	 */
	@RequestMapping(value = "/add.htm", method = RequestMethod.GET)
	public ModelAndView add(@ModelAttribute SysRegionForm model,HttpServletRequest request,HttpServletResponse response,HttpSession session){
		ModelAndView view = new ModelAndView();
		view.setViewName("/admin/system/sys_region_area_add");
		Map<String, Object> data = view.getModel();
		SysRegionCondition condition = new SysRegionCondition();
		/*condition.setFlag(1);*/
		condition.setParentId(0L);
		data.put("regionList",sysRegionService.listSysregionByCondition(condition));
		return view;
	}
	/**
	 * 跳转到修改页面
	 * @return 
	 */
	@RequestMapping(value = "/update.htm", method = RequestMethod.GET)
	public ModelAndView update(@ModelAttribute SysRegionForm model,HttpServletRequest request,HttpServletResponse response,HttpSession session){
		ModelAndView view = new ModelAndView();
		view.setViewName("/admin/system/sys_region_area_add");
		Map<String, Object> data = view.getModel();
		SysRegionArea item = sysRegionAreaService.getSysRegionAreaById(model.getAreaId());
		SysRegionCondition condition = new SysRegionCondition();
		/*condition.setFlag(1);*/
		condition.setParentId(0L);
		data.put("regionList",sysRegionService.listSysregionByCondition(condition));
		data.put("item",item);
		return view;
	}
	/**
	 * 保存大区管理表
	 * @return 
	 */
	@RequestMapping(value = "/doAdd.htm", method = RequestMethod.POST)
	public ModelAndView doAdd(@ModelAttribute SysRegionAreaForm model,HttpServletRequest request,HttpServletResponse response,HttpSession session){
		try {
			sysRegionAreaService.add(model);
			return ajaxJSON(Status.success, "添加成功");
		} catch (Exception e) {
			String msg = "保存大区管理表：" + e.getMessage();
			logger.error(msg,e);
			return ajaxJSON(Status.error, "保存失败，请稍后重试");
		}
	}
	/**
	 * 分页查询
	 * @return 
	 */
	@RequestMapping(value = "/listSysRegionAreaByPage.htm", method = RequestMethod.POST)
	public void listSysRegionAreaByPage(@ModelAttribute SysRegionAreaForm model,HttpServletRequest request,HttpServletResponse response,HttpSession session){
		try{
			sysRegionAreaService.listSysRegionAreaByPage(model.getPageResult(),model.getCondition());
			JSONObject root = toPageJson(model.getPageResult(), new String[]{"id","name","sort","createDate"});
			ajaxPageResult(root,response);
		}catch(Exception ex){
			String msg = "查询大区管理时发生异常："+ex.getMessage();
			logger.error(msg,ex);
		}
	}
	/**
	 * 查看已选的区域
	 * @param model
	 * @param request
	 * @param response
	 * @param session
	 */
	@RequestMapping(value = "/alreadySelectArea.htm",method = RequestMethod.GET)
	public ModelAndView alreadySelectArea(@ModelAttribute SysRegionForm model,HttpServletRequest request,HttpServletResponse response,HttpSession session){
		ModelAndView view = new ModelAndView();
		view.setViewName("/admin/system/sys_region_area_detail");
		Map<String, Object> data = view.getModel();
		data.put("areaId", model.getAreaId());
		return view;
	}
	
	/**
	 * 查看需要选择的区域
	 * @param model
	 * @param request
	 * @param response
	 * @param session
	 */
	@RequestMapping(value = "/selectArea.htm",method = RequestMethod.GET)
	public ModelAndView selectArea(@ModelAttribute SysRegionForm model,HttpServletRequest request,HttpServletResponse response,HttpSession session){
		ModelAndView view = new ModelAndView();
		view.setViewName("/admin/system/sys_region_area_detail");
		Map<String, Object> data = view.getModel();
		data.put("areaId", model.getAreaId());
		data.put("flag", "1");
		return view;
	}
	
	/**
	 * 保存大区管理
	 * @return 
	 */
	@RequestMapping(value = "/edit.htm", method = RequestMethod.GET)
	public ModelAndView edit(@ModelAttribute SysRegionAreaForm model,HttpServletRequest request,HttpServletResponse response,HttpSession session){
		ModelAndView view = new ModelAndView();
		view.setViewName("");
		Map<String,Object> data = view.getModel();
		if(StringUtils.isNotBlank(model.getItem().getId())){
			SysRegionArea item = sysRegionAreaService.getSysRegionAreaById(model.getItem().getId());
			data.put("item", item);
		}

		return view;
	}

	/**
	 * 保存大区管理
	 * @return 
	 */
	@RequestMapping(value = "/saveSysRegionArea.htm", method = RequestMethod.POST)
	@TokenValidate
	public ModelAndView saveRole(@ModelAttribute SysRegionAreaForm model,HttpServletRequest request,HttpSession session){
		try {
			if(StringUtils.isBlank(model.getItem().getId())){
				sysRegionAreaService.add(model);
			}else{
				sysRegionAreaService.update(model.getItem());
			}
			return ajaxJSON(Status.success,"保存成功");
		} catch (Exception e) {
			String msg = "保存大区管理时发生异常：" + e.getMessage();
			logger.error(msg,e);
			return ajaxJSON(Status.error, "保存失败，请稍后重试");
		}
	}
	/**
	 * 删除SysRegionArea
	 * @return 
	 */
	@RequestMapping(value = "/del.htm", method = RequestMethod.POST)
	public ModelAndView del(@ModelAttribute SysRegionAreaForm model,HttpServletRequest request,HttpServletResponse response,HttpSession session){
		try {
			sysRegionAreaService.delByIds(model.getSelIds().split(","));
			return ajaxJSON(Status.success,"删除成功");
		} catch (Exception e) {
			String msg = "删除大区管理时发生异常：" + e.getMessage();
			logger.error(msg,e);
			return ajaxJSON(Status.error, "删除失败，请稍后重试");
		}
	}



}

